<template>
  <el-form ref="form" :rules="rules" :model="user" :hide-required-asterisk="true">
    <el-form-item label="昵称" prop="name">
      <el-input v-model.trim="user.name" />
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model.trim="user.email" />
    </el-form-item>
    <el-form-item>
      <el-button :loading="loading" type="primary" @click.native.prevent="submit">更新</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { validEmail } from '@/utils/validate'

export default {
  props: {
    user: {
      type: Object,
      default: () => {
        return {
          name: '',
          email: ''
        }
      }
    }
  },
  data() {
    const validateEmail = (rule, value, callback) => {
      if (!validEmail(value)) {
        callback(new Error('请输入正确的邮箱'))
      } else {
        callback()
      }
    }
    return {
      rules: {
        name: [{ required: true, message: '请输入昵称', trigger: ['blur', 'change'] }],
        email: [{ required: true, message: '请输入邮箱', trigger: ['blur', 'change'] }, { validator: validateEmail }]
      },
      loading: false
    }
  },
  methods: {
    submit() {
      // this.loading = true
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$message({
            message: '基本信息更新成功',
            type: 'success',
            duration: 5 * 1000
          })
        }
      })
    }
  }
}
</script>
